import 'dart:convert';
import 'package:flutter/material.dart';

import '../Pages/DeviceSetting/all_collapse/light_collapse.dart';

class ShowModeDescription extends StatelessWidget {
  final String mode;

  const ShowModeDescription({super.key, required this.mode});

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> modeData = {};
    try {
      modeData = jsonDecode(mode);
    } catch (e) {
      print("解析错误，${e.toString()}");
      return SizedBox(height: 10);
    }
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildLightModeSetting(modeData),
        SizedBox(height: 10),
        _buildGroupSetting(modeData),
        SizedBox(height: 10),
        _buildRFSetting(modeData),
      ],
    );
  }

  // 亮灯模式配置
  _buildLightModeSetting(Map<String, dynamic> modeData) {
    if (modeData['lightmode_setting'] == 0) {
      return Container();
    }
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Center(
          child: Text(
            "亮灯模式配置",
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
        SizedBox(height: 10),
        Text(
          "- 灯光模式：${modeData['mode'] == 0 ? '自动' : '手动'}",
          style: TextStyle(fontSize: 18),
        ),
        if (modeData['mode'] == LightModeEnum.manual) ...[
          Text(
            "- 固定亮度：${modeData['fixedBrightness']}",
            style: TextStyle(fontSize: 18),
          ),
        ] else ...[
          Text(
            "- 有人亮度：${modeData['peopleBrightness']} %",
            style: TextStyle(fontSize: 18),
          ),
          Text(
            "- 无人亮度：${modeData['unmannedBrightness']} %",
            style: TextStyle(fontSize: 18),
          ),
          Text(
            "- 灭灯延迟时间：${modeData['dark_delay']} 秒",
            style: TextStyle(fontSize: 18),
          ),
          Text(
            "- 亮灯延迟时间：${modeData['lightDelayTime']} 秒",
            style: TextStyle(fontSize: 18),
          ),
        ],
      ],
    );
  }

  // 分组模式配置
  _buildGroupSetting(Map<String, dynamic> modeData) {
    if (modeData['group_setting'] == 0) {
      return Container();
    }
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Center(
          child: Text(
            "分组设置",
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
        SizedBox(height: 10),
        Text("- 第一组号：${modeData['new_group']}", style: TextStyle(fontSize: 18)),
        if (modeData['is_second_group'] ?? false)
          Text(
            "- 第二组号：${modeData['second_group_id']}",
            style: TextStyle(fontSize: 18),
          ),
      ],
    );
  }

  // RF模式配置
  _buildRFSetting(Map<String, dynamic> modeData) {
    if (modeData['rf_setting'] == 0) {
      return Container();
    }
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Center(
          child: Text(
            "RF设置",
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
        SizedBox(height: 10),
        Text(
          "- 转发：${modeData['forwarding'] == 0 ? '关闭' : '开启'}",
          style: TextStyle(fontSize: 18),
        ),
        SizedBox(height: 10),
        Text("- TTL值：${modeData['ttl_count']}", style: TextStyle(fontSize: 18)),
        Text(
          "- 网络：${modeData['network'] == 0 ? '关闭' : '开启'}",
          style: TextStyle(fontSize: 18),
        ),
        Text("- 发射功率：${modeData['tx_power']}", style: TextStyle(fontSize: 18)),
      ],
    );
  }
}
